﻿using System;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace AngryManagementMayhem.Controller.Map
{
    public class CollisionDefinition
    {
        public const int CollisionSize = 32;

        public enum CellTypeList
        {
            Terrain,
            Hazard,
            Ladder,
            Door,
        }

        public CellTypeList CellType { get; private set; }

        [Flags]
        public enum CollisionTypeList
        {
            Ceiling = 8,
            Floor = 4,
            Right = 2,
            Left = 1,
            None = 16
        }

        public CollisionTypeList CollisionType { get; private set; }

        public enum SpecialTileEffectList
        {
            None,
            RiseForStairs,
        }

        public SpecialTileEffectList CollisionEffect { get; private set; }

        public TE_Vector2 Position { get; set; }

        public CollisionDefinition()
            : this(CollisionTypeList.Floor)
        {
            
        }

        public CollisionDefinition(CollisionTypeList collisionType, CellTypeList cellType = CellTypeList.Terrain, SpecialTileEffectList collisionEffect = SpecialTileEffectList.None)
        {
            CellType = cellType;
            CollisionType = collisionType;
            CollisionEffect = collisionEffect;
        }

        public static bool ContainsType(CollisionTypeList collisionType, CollisionTypeList collision)
        {
            return (collisionType & collision) == collision;
        }
        public bool ContainsType(CollisionTypeList collision)
        {
            return ContainsType(CollisionType, collision);
        }

        public TE_Vector2 ResolveCollision(TE_Vector2 objectPosition, TE_Vector2 direction, CollisionTypeList collisionType)
        {

            switch (collisionType)
            {
                case CollisionTypeList.Left:
                    if(direction.X > 0)
                     return new TE_Vector2(Position.X - objectPosition.X, 0);
                    break;
                case CollisionTypeList.Right:
                    if(direction.X < 0)
                     return new TE_Vector2(Position.X + CollisionSize - objectPosition.X, 0);
                    break;
                case CollisionTypeList.Floor:
                    if (direction.Y < 0)
                        break;
                    return new TE_Vector2(0, Position.Y - objectPosition.Y);
                case CollisionTypeList.Ceiling:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return null;
        }

        public override string ToString()
        {
            return string.Format("[{0}, {1}, {2}]", CellType, CollisionType, CollisionEffect);
        }

        public bool IsEqualTo(CollisionDefinition c1)
        {
            return Position.X == c1.Position.X && Position.Y == c1.Position.Y;
        }
    }
}